#include "MainGame.h"
#include "RSManager.h"
#include "FWGame.h"
#include "FWKeyboard.h"
#include "FWGraphic.h"
#include "FWContentManager.h"
#include "FWTexture.h"
#include "ShareComponents.h"

#include "Penguin.h"
#include "Physics.h"
#include "Camera.h"
#include "FlockOfPenguin.h"
#include "BackGround.h"
#include "Body.h"
#include "LittleFairy.h"
#include "World.h"
#include "Bear.h"
#include "Snow.h"
#include "Cloud.h"
MainGame::MainGame(HINSTANCE hIns,char* windowName,int w,int h):FWGame(hIns,windowName,w,h)
{
	D3DXMatrixIdentity(&this->matrix_transform);
}

MainGame::~MainGame(void)
{
}
void MainGame::load()
{	
	_world = new World();
	_camera = new Camera(0, 2000, 480);

	this->keyboard = new FWKeyboard(this);	
	gl_keyboard = keyboard;
	if(!keyboard->init()) MessageBox(NULL,"Error keyboard!","Error!",MB_OK);		


	this->graphic = new FWGraphic(this);		
	graphic->init();	
	gl_graphic = graphic;

	content  = new FWContentManager(this);
	gl_content = content;

	RSManager::getInstance()->Init();	

	pen = new Penguin();
	Body* body = new Body();
	body->init();	
	pen->addComponent(body);	
	Physics* vatly = new Physics(D3DXVECTOR2 (20,0));	
	vatly->setV(1.5f,0);
	pen->addComponent(vatly);

	_backGround = new BackGround();	
	_world->insert(_backGround);
	Bear* bear = new Bear();
	_world->insert(bear);

	bayCanhCut = new FlockOfPenguin();
	for (int i = 0 ; i < 5; i++)
	{
		GameObject* c = new Penguin();
		Body* b = new Body();
		b->init();
		c->addComponent(b);
		Physics* v = new Physics(D3DXVECTOR2 (20 + i * 30 + 30,0));
		v->init();
		v->setV(1.5f,0);
		c->addComponent(v);
		bayCanhCut->addObject(c);
	}
	_world->insert(bayCanhCut);


	bayCanhCut = new FlockOfPenguin();
	for (int i = 0 ; i < 5; i++)
	{
		GameObject* c = new Penguin();
		Body* b = new Body();
		b->setSprite(GO_LEFT);
		c->addComponent(b);
		Physics* v = new Physics(D3DXVECTOR2 (500 + i * 30 + 30,0));		
		v->setV(-1.5f,0);
		c->addComponent(v);
		bayCanhCut->addObject(c);
	}

	_world->insert(bayCanhCut);
	//
	lf = new LittleFairy();
	body = new Body();
	body->setSprite(LITTLE_FAIRY);
	vatly = new Physics(D3DXVECTOR2(20,100));	
	vatly->setV(0,2);
	lf->addComponent(body);
	lf->addComponent(vatly);
	_world->insert(lf);

	lf1 = new LittleFairy();
	body = new Body();
	body->setSprite(ANGEL);
	vatly = new Physics(D3DXVECTOR2(100,200));	
	vatly->setV(0,-2);
	lf1->addComponent(body);
	lf1->addComponent(vatly);
	_world->insert(lf1);



	lf1 = new LittleFairy();
	body = new Body();
	body->setSprite(ANGEL);
	vatly = new Physics(D3DXVECTOR2(180,300));	
	vatly->setV(0,2);
	lf1->addComponent(body);
	lf1->addComponent(vatly);
	_world->insert(lf1);

	vatly = new Physics(D3DXVECTOR2(-2000,380));
	cloud = new Cloud(vatly);
	_world->insert(cloud);
	vatly = new Physics(D3DXVECTOR2(0,380));
	cloud = new Cloud(vatly);
	_world->insert(cloud);



	lf1 = new LittleFairy();
	body = new Body();
	body->setSprite(ANGEL);
	vatly = new Physics(D3DXVECTOR2(280,200));	
	vatly->setV(1,-2);
	lf1->addComponent(body);
	lf1->addComponent(vatly);
	_world->insert(lf1);




	FlockOfPenguin* snows = new FlockOfPenguin();
	for(int i=0;i<400;i++)
	{
		int y=rand()%1000;
		GameObject* snow = new Snow();
		Body* b = new Body();
		b->setSprite(SNOW);
		Physics* v = new Physics(D3DXVECTOR2(5*i,y));
		v->setV(0,-1);
		snow->addComponent(b);
		snow->addComponent(v);
		snows->addObject(snow);
	}
	_world->insert(snows);


}

void MainGame::render()
{	
	graphic->clear(D3DCOLOR_XRGB(255,255,255));		
	graphic->begin(_camera->getTransformedMatrix());
	//_backGround->render();

	//bayCanhCut->render();
	_world->render();
	pen->render();
	//lf->render();
	//lf1->render();
	graphic->end();
}

void MainGame::shutdown()
{

}

void MainGame::update(float time)
{		
	_camera->Update(pen->getPosition());
	keyboard->getState();	
	pen->update();
	//bayCanhCut->update();
	_world->update();
	//lf->update();
	//lf1->update();
	keyboard->saveState();
}